import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.*;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;
//Current Status - The manufacturer method is working. 
//TO DO - Make the other methods return an arraylist for the drop down menu to keep updating.
//TO DO - Make the vendor to be working to.
// TO DO - Insertion,deletion, Update - Add a text area at LAyout.SOUTH

public class Insert extends JFrame 
{
	ArrayList<String> values2 = new ArrayList<String>();// The selected values.
	private String selected;
	private JTextField displayField = new JTextField(30);
	private JComboBox c = new JComboBox();
	private int count = 0,insert_count = 0;
	private String tableName = "";
	private ArrayList<MainManuf> cart = new ArrayList<MainManuf>();
	private Connection con;
	private ConnectDb conDb;
	final JTextArea carts = new JTextArea();
	final JFrame frame ;
	String values= "";

	public Insert(final Connection connection, final ConnectDb connectDb) 
	{
		setLayout(new FlowLayout());
		con = connection;
		conDb = connectDb;
		frame = new JFrame();
		final Query q = new Query(con, conDb);  
		final ArrayList<String> temp = q.queryManufac();
		final int x = temp.size();

		JButton b = new JButton("Add to Cart");
		for(int i = 0;i<x;i++)
			c.addItem(temp.get(i));

		displayField.setEditable(false);
		c.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//	count++;
				selected = temp.get(c.getSelectedIndex());
			}
		});

		final JTextField year = new JTextField("year",4);
		final JTextField litres = new JTextField("litres",4);
		final JTextField engine = new JTextField("engine",4);
		final JTextField desc = new JTextField("desc",5);
		final JTextField cubic = new JTextField("cubic",3);
		final JTextField rlink = new JTextField("rlink",4);
		final JTextField model = new JTextField("model");


		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {    
				//c.removeAllItems();
				ArrayList<String> params = new ArrayList<String>();

				params.add(model.getText());
				params.add(year.getText());
				params.add(desc.getText());
				params.add(litres.getText());
				params.add(engine.getText());
				params.add(cubic.getText());
				params.add(rlink.getText());

				/*The Querying will start from here.*/
				String query = "Select tname from manuf where manuf='" + selected + "'";

				System.out.println(query);
				try
				{
					Statement stmnt2 = connection.createStatement();
					ResultSet rs = stmnt2.executeQuery(query);

					while(rs.next())
						tableName = rs.getString(1); //gets the tablename from dan's MANUF
				}
				catch (SQLException ex) 
				{
					System.err.println(ex.getMessage());
				}
				for(int i = 0; i < params.size(); i++){

					if(i==params.size()-1){
						values += "'" + params.get(i).trim()+ "'";
					}
					else{
						values += "'" + params.get(i).trim()+ "',";

					} 
				}

				System.out.println(values+" Values");
				values2.add(values);
				values = "";
				//the cart adder.
				count++;
				MainManuf m = new MainManuf(selected,params.get(1),params.get(2),params.get(3),params.get(4),params.get(5),params.get(6));
				cart.add(m);
				String temp_cart = "Your cart Contains: \n";
				for(int i = 0;i<cart.size();i++)
					temp_cart += (cart.get(i).toString())+"\n";
				temp_cart += "Total Items: "+count;
				carts.setText(temp_cart);
				//cart adder ends here.
			}
		});

		//b.add(comp)

		JPanel optionPanel = new JPanel() ; // the 2nd panel which displays the content.
		JPanel text = new JPanel();

		JButton exit = new JButton("Exit");
		exit.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		JButton undo = new JButton("Undo");
		undo.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) {
				if(count < 1)
				{
					JOptionPane.showMessageDialog(frame, "You cannot undo anymore Items!");
				}
				else
				{
					int x = cart.size()-1; //last element in the list.
					delete(tableName, cart.get(x).getModel(),cart.get(x).getEngine(),cart.get(x).getYear(),cart.get(x).getRlink());
					cart.remove(cart.size()-1);
					count--;
					String temp_cart = "Your cart Contains: \n";
					for(int i = 0;i<cart.size();i++)
						temp_cart += (cart.get(i).toString())+"\n";
					temp_cart += "Total Items: "+count;
					carts.setText(temp_cart);
				}
			}
		});


		optionPanel.setLayout(new FlowLayout());
		text.setLayout(new FlowLayout());
		text.add(year);
		text.add(litres);
		text.add(engine);
		text.add(desc);
		text.add(cubic);
		text.add(rlink);
		text.add(carts);

		optionPanel.add(c);
		optionPanel.add(b);

		//ExitP panel.
		JButton insertall = new JButton("Insert All");
		insertall.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) {
				//for(int i =0;i<values2.size();i++)
				insert(values2.get(insert_count));
				insert_count++;
				JOptionPane.showMessageDialog(frame, "All items have been added!");
			}
		});
		JPanel exitP = new JPanel();
		exitP.add(exit);
		exitP.add(undo);
		exitP.add(insertall);

		//Frame and BorderLayouts
		frame.setLayout(new BorderLayout());
		frame.add(exitP,BorderLayout.SOUTH);
		frame.add(optionPanel,BorderLayout.NORTH);
		frame.add(text, BorderLayout.CENTER);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 400);
		frame.setVisible(true);
	}

	public void insert(String val)
	{
		System.out.println("Inside insert: "+val);
		String query2 = "Insert into " + tableName + " values ("+ val+")";
		try
		{

			Statement stmnt2 = con.createStatement();
			int suc = stmnt2.executeUpdate(query2);
			if(suc >0){
				JOptionPane.showMessageDialog(frame,"Update Success!");
			}
		}
		catch (SQLException ex) 
		{
			System.err.println(ex.getMessage());
		}
	}

	public void delete(String tname, String model,String engine,String year,String rlink)
	{
		String query = "DELETE FROM "+ tname+ "WHERE MODEL = '"+ model+"' AND ENGINE_TYPE = '" +engine+"' AND YEAR = '"+year+"' AND RLINK = '"+rlink+"'";
		try
		{          
			Statement stmnt = con.createStatement();
			int res = stmnt.executeUpdate(query);
			System.out.println(res);
		}

		catch (SQLException ex) 
		{
			System.err.println(ex.getMessage());
		}
	}


}

